package fr.paulmasson.jfreex;

import java.awt.Frame;

import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;

import com.javadude.swt.layouts.BorderLayout;

import fr.paulmasson.jfreex.channels.Free;
import fr.paulmasson.jfreex.db.DbManager;
import fr.paulmasson.jfreex.db.dao.DaoChannelInfo;
import fr.paulmasson.jfreex.db.dao.DaoProperty;
import fr.paulmasson.jfreex.db.dao.DaoProvider;
import fr.paulmasson.jfreex.replay.Replay;
import fr.paulmasson.jfreex.ui.ChannelsList;
import fr.paulmasson.jfreex.ui.InfosPanel;
import fr.paulmasson.jfreex.ui.Links;
import fr.paulmasson.jfreex.ui.ReplayList;
import fr.paulmasson.jfreex.ui.VlcPlayer;

public class JFreeX {
	
	static Display display;
	static Shell shell;

	public static void main(String[] args) {
		
		
		
		new JFreeX();

	}

	private Composite livePanel;
	private TabItem liveTab;
	private VlcPlayer player;
	private ChannelsList free_channelList;
	private InfosPanel infoPanel;
	
	private boolean fullscreen = false;
	private TabFolder tabs;
	private Composite videoComposite;
	private Composite infoPanelReal;
	private Composite replayPanel;
	private TabItem replayTab;
	private Browser browser;
	private ReplayList replay_list;
	private Shell splash;
	private ProgressBar bar;
	private String desc = "";
	
	public JFreeX() {
		display = new Display();
		this.createSplashScreen();

		desc = "Init DB";
		bar.setSelection(10);
		DbManager.startDbServer();
		DaoChannelInfo.setup();
		DaoProvider.setup();
	
		

		desc = "Init Frame";
		bar.setSelection(20);
		this.initFrame();
		desc = "Init Channels";
		bar.setSelection(30);
		this.initFreeChannels();
		desc = "Init Replays";
		bar.setSelection(40);
		this.initReplayChannels();
		desc = "Init Player";
		bar.setSelection(50);
		this.initVlcPlayer();
		desc = "Init Browser";
		bar.setSelection(60);
		this.initReplayBrowser();
		desc = "Init Info Panel";
		bar.setSelection(70);
		this.initInfoPanel();
		desc = "Init Links";
		bar.setSelection(80);
		new Links(player, infoPanel, free_channelList);
		new Links(browser,replay_list);

		desc = "Add component";
		bar.setSelection(90);

		liveTab.setControl(livePanel);
		replayTab.setControl(replayPanel);
		tabs.pack();

		desc = "Display";
		bar.setSelection(100);

		
		int width = display.getPrimaryMonitor().getBounds().width;
		int height = display.getPrimaryMonitor().getBounds().height;
		

		shell.setLocation(
				width/2-shell.getBounds().width/2+display.getPrimaryMonitor().getBounds().x,
				height/2-shell.getBounds().height/2+display.getPrimaryMonitor().getBounds().y);
		
		shell.open();
		splash.dispose();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
		

		System.exit(0);
		DbManager.stopDbServer();
	}

	private void createSplashScreen() {
		
		System.out.println("create splash screen");
		splash = new Shell(SWT.ON_TOP);
		
		int width = display.getPrimaryMonitor().getBounds().width;
		int height = display.getPrimaryMonitor().getBounds().height;
		
		bar = new ProgressBar(splash, SWT.NONE);
	    bar.setMaximum(100);
	    Label label = new Label(splash, SWT.NONE);
	    label.setImage(new Image(display,this.getClass().getResource("/data/splash.jpg").toString().replaceAll("file:", "")));
	    FormLayout layout = new FormLayout();
	    splash.setLayout(layout);
	    
	    FormData labelData = new FormData();
	    labelData.right = new FormAttachment(100, 0);
	    labelData.bottom = new FormAttachment(100, 0);
	    label.setLayoutData(labelData);
	    
	    FormData progressData = new FormData();
	    progressData.left = new FormAttachment(0, 165);
	    progressData.right = new FormAttachment(100, -164);
	    progressData.bottom = new FormAttachment(100, -57);
	    bar.setLayoutData(progressData);
	    bar.addPaintListener(new PaintListener() {
			
			@Override
			public void paintControl(PaintEvent e) {
				String string = desc;
					  Point point = bar.getSize();
					  FontMetrics fontMetrics = e.gc.getFontMetrics();
					  int width = 
					   fontMetrics.getAverageCharWidth() * string.length();
					  int height = fontMetrics.getHeight();
					  e.gc.setForeground
					 (display.getSystemColor(SWT.COLOR_WHITE));
					  e.gc.drawString
					   (string, (point.x-width)/2 , (point.y-height)/2, true);
			}
		});
	    
	    
	    splash.pack();

		splash.setLocation(
				width/2-splash.getBounds().width/2+display.getPrimaryMonitor().getBounds().x,
				height/2-splash.getBounds().height/2+display.getPrimaryMonitor().getBounds().y);
		splash.open();
	}

	private void initReplayBrowser() {
		browser = new Browser(replayPanel, SWT.BORDER);
		browser.setLayoutData(BorderLayout.CENTER);
	}

	private void initReplayChannels() {
		replay_list = new ReplayList(); 
		ReplayList.create(replayPanel, Replay.replay_channels).setLayoutData(BorderLayout.WEST);
		
	}

	private void initInfoPanel() {
		infoPanel = new InfosPanel(); 
		infoPanelReal = infoPanel.create(livePanel, display);
		infoPanelReal.setLayoutData(BorderLayout.SOUTH);
	}

	private void initVlcPlayer() {
		videoComposite = new Composite(livePanel, SWT.EMBEDDED);
		videoComposite.setLayoutData(BorderLayout.CENTER);
		Frame videoFrame = SWT_AWT.new_Frame(videoComposite);
		videoComposite.setVisible(true);
		player = new VlcPlayer(videoFrame,this,videoComposite.getBackground());
		videoComposite.setBounds(100, 100, 450, 200);
		player.playMedia(this.getClass().getResource("/data/splash.jpg").toString().replaceAll("file:",""));

	}

	private void initFreeChannels() {
		free_channelList = new ChannelsList(); 
		ChannelsList.create(livePanel, Free.free_channels).setLayoutData(BorderLayout.WEST);
	}

	private void initFrame() {
		
		shell = new Shell(display);
		
		shell.setImage(new Image(display,this.getClass().getResource("/data/icon.png").toString().replaceAll("file:", "")));
		
		shell.setLayout(new BorderLayout(5, 5));
	
		
		shell.setText("JFreeX");
		Display.setAppVersion("1.0.0");
		Display.setAppName("JFreeX");
		
		tabs = new TabFolder(shell, SWT.BORDER);


		liveTab = new TabItem(tabs, SWT.NONE);
		liveTab.setText("Free Live Channels");
		livePanel = new Composite(tabs, SWT.NONE);
		livePanel.setLayout(new BorderLayout(5,5));
		
		replayTab = new TabItem(tabs, SWT.NONE);
		replayTab.setText("Replay");
		replayPanel = new Composite(tabs, SWT.NONE);
		replayPanel.setLayout(new BorderLayout(5,5));
		
		
		
		tabs.setLayoutData(BorderLayout.CENTER);
		
		
		tabs.addSelectionListener(new SelectionListener() {
			
			@Override
			public void widgetSelected(SelectionEvent arg0) {
				livePanel.layout(true);
				replayPanel.layout(true);
			}
			
			@Override
			public void widgetDefaultSelected(SelectionEvent arg0) {
			}
		});
	}
	
	public boolean isFullscreen() {
		return fullscreen;
	}

	public void fullscreen() {
		display.syncExec(new Runnable() {
		    public void run(){
				fullscreen = !fullscreen;
		    	shell.setFullScreen(fullscreen);
		    	if (fullscreen) {
		    		tabs.setVisible(false);		    		
		    		videoComposite.setParent(shell);
		    		shell.layout(true);
		    	} else {
		    		tabs.setVisible(true); 		
		    		videoComposite.setLayoutData(BorderLayout.CENTER);
		    		infoPanelReal.setLayoutData(BorderLayout.SOUTH);
		    		videoComposite.setParent(livePanel);
		    		infoPanelReal.setParent(livePanel);
		    		livePanel.layout(true);
		    		shell.layout(true);
		    		
		    	}
		    }
		});
		
	}

}
